function [ImgPairs, PSNRs, SSIMs, iters] = deployImgsByIterModels(ImgPairs, net, iters, iters2, solver_mode)
%%
% net is structure with fields:
%    .model_path
%    .model_prefix
%    .deploy_file

%%
global DeployImgs_LogFile
nIn = nargin;

PSNRs = []; SSIMs = [];
if ischar(net)
    model_path = net;
    net = struct;
    net.model_path = model_path;
    
    net_path = fileparts(net.model_path);
    st = dir(fullfile(net_path, '*_deploy.prototxt'));
    if length(st) > 1, error('More than one deploy file!'); end
    net.deploy_file = fullfile(net_path, st(1).name);
    
    st = dir(fullfile(net.model_path, '*.caffemodel'));
    if isempty(st)
        iters = [];
        return;
    end
    net.model_prefix = mycaffe.get_snapshot_prefix(st(1).name);
end

if nIn < 4 || isempty(iters) || strcmpi(iters, 'all')
    [model_files, iters] = mycaffe.splitModelFiles(net.model_path);
    model_files = [model_files{:}];  iters = [iters{:}];
else
    [model_files, iters] = mycaffe.getModelFiles(net.model_path, net.model_prefix, iters);
end

if isempty(iters), return; end

if nIn > 4 && ~isempty(iters2)
    [iters, idx] = setdiff(iters, iters2);
    model_files  = model_files(idx);
end

if nIn < 6, solver_mode = 'gpu 0'; end

ImgNum = size(ImgPairs, 1);
hPfrintf = mycaffe.getPrintf(DeployImgs_LogFile);

%% Warm Starting
[ModelLink, scale] = srdata.getModelLink(net.model_path);
ModelNum = size(ModelLink, 1);
if ModelNum > 0, hPfrintf('SR Warm Starting ...\n'); end
PSNRs0 = zeros(ImgNum, ModelNum);  SSIMs0 = PSNRs0;
for j = 1 : ModelNum
    hDeploy = @(x) mycaffe.caffe(x, ModelLink{j, :}, 'test');
    [ImgPairs, PSNRs0(:, j), SSIMs0(:, j)] = srmodel.deployPairs(ImgPairs, scale, solver_mode, hDeploy, @(x, y) y);
    hPfrintf('\tModel File: %s, Mean PSNR: %.3f\n', ModelLink{j, 2}, mean(PSNRs0(:, j)));
end

%%
ModelNum = length(model_files);
if ModelNum == 0
    PSNRs = [];  SSIMs = [];
    hPfrintf('Caffe model in "%s" is empty!', strrep(net.model_path, '%', '%%'))
    return
end

%%
hPfrintf('SR Forward Processing ...\n');
PSNRs = zeros(ImgNum, ModelNum);  SSIMs = PSNRs;
% mycaffe.init(solver_mode)
for j = 1 : ModelNum
    caffemodel = fullfile(net.model_path, model_files{j});
    hDeploy = @(x) mycaffe.caffe(x, net.deploy_file, caffemodel, 'test');
    [DeployedImgPairs, PSNRs(:, j), SSIMs(:, j)] = srmodel.deployPairs(ImgPairs, scale, solver_mode, hDeploy, @(x, y) y);
    hPfrintf('\tModel %d / %d, Model File: %s, Mean PSNR: %.3f\n', j, ModelNum, model_files{j}, mean(PSNRs(:, j)));
end
ImgPairs = DeployedImgPairs;
